package org.brain.servlet;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Response;
 
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.brain.pojos.User;
import org.brain.tool.ToolBean;
 
public class FileUpload extends HttpServlet {
    /**
	 * 
	 */
	private static final long serialVersionUID = -7468226186058540868L;
	private String uploadPath = ""; // 上传文件的目录
    private String tempPath = ""; // 临时文件目录
    File tempPathFile;
 
    public void doPost(HttpServletRequest request, HttpServletResponse response)
           throws IOException, ServletException {
    			User user = (User)request.getSession().getAttribute("user");
    			uploadPath = request.getRealPath("/")+"pic\\"+user.getUsername();
    			tempPath = request.getRealPath("/")+"pic\\buffer\\";
    			init_dir(uploadPath);
    		response.setContentType("text/html");
    		response.setCharacterEncoding("utf-8");
    		PrintWriter out = response.getWriter();
       try {
           // Create a factory for disk-based file items
           DiskFileItemFactory factory = new DiskFileItemFactory();
 
           // Set factory constraints
           factory.setSizeThreshold(4096); // 设置缓冲区大小，这里是4kb
           //factory.setRepository(tempPathFile);// 设置缓冲区目录
 
           // Create a new file upload handler
           ServletFileUpload upload = new ServletFileUpload(factory);
 
           // Set overall request size constraint
           upload.setSizeMax(4194304); // 设置最大文件尺寸，这里是4MB
 
           List<FileItem> items = upload.parseRequest(request);// 得到所有的文件
           Iterator<FileItem> i = items.iterator();
          while (i.hasNext()) {
              FileItem fi = (FileItem) i.next();
              String fileName = fi.getName();
              if (fileName != null) {
                  File fullFile = new File(fi.getName());
                  File savedFile = new File(uploadPath, fullFile.getName());
                  fi.write(savedFile);
              }
           }
           out.println("<img width='40px' src='images/icons/right.png' />图片提交成功！");
           out.println("<a href='FileUpload.jsf'>确定</a>");
          // response.sendRedirect("/FileUpload.jsf");
           out.flush();
           out.close();
           
           
       } catch (Exception e) {
           // 可以跳转出错页面
           e.printStackTrace();
       }
    }
 
    public void init_dir(String uploadPath){
       File uploadFile = new File(uploadPath);
       if (!uploadFile.exists()) {
           uploadFile.mkdirs();
       }
       File tempPathFile = new File(tempPath);
        if (!tempPathFile.exists()) {
           tempPathFile.mkdirs();
       }
    }
}